// page_two/txdrug/txdrug.js
let app = getApp()
let yuming = app.globalData.yuming
let shuzi = app.globalData.shuzi
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '特药报备',
    buttonClicked: false,
    drugs_type: 1, //1五类药，2特效药
    // huan: ['台账信息', '销售信息'],
    huan: ['五类药品', '含麻黄碱类复方制剂'],
    currtab: 0,
    page: 1,
    yao_guo: '',
    satrtime: '',
    endtime: '',
    satrtimes: '开始时间',
    endtimes: '结束时间',
    // yaolist: ['五类药品', '含麻黄碱类复方制剂'],
    yaolist: ['五类药品', '含麻黄碱类复方制剂'],
    // tsyList: [{
    //   createtime: '2022-04-21',
    //   yptype: '麻黄碱',
    //   ypname: '甲硝唑胶囊',
    //   gg: '规格',
    //   tm: '条码',
    //   sccs: '生产厂商',
    //   scdate: '生产日期',
    //   yxqz: '有效期至',
    //   cpph: '产品批号',
    //   images: [`${yuming}static/applet/images/xiaofei/images/linshi/wyfw.jpg`, `${yuming}static/applet/images/xiaofei/images/linshi/yxc.jpg`, `${yuming}static/applet/images/xiaofei/images/linshi/wyn.jpg`]
    // }],
    tsyList: [],
    xiaoshoulist: [],
    yptypeindex: 0,
    yptypelist: ['麻黄碱', '处方药'],
    yptype: '请选择药品类型',
    ypname: '',
    gg: '',
    tm: '',
    sccs: '',
    scdate: '请选择生产日期',
    yxqz: '请选择有效期截止时间',
    cpph: '',
    photo: [],
    wuphoto: '',
    date: '选择时间', //开始
    dateTime: '',
    istime: false, //弹出时间筛选
    gongwu: false,
    xuany_index: 0,
    keywords: '',


    is_shai: false,
    yao_ming: '',
    study_status: '',
    yao_pihao: '',
    yao_guige: '',
    yao_fenl: '',
    yao_scdw: '',
    yao_gong: '',
    yao_cai: '',
    yao_jinnum: '',
    yao_jintime: '',
    yao_youx: '',
    yap_piwenh: '',
    isxuan: false,
    isxuans: false,
    jinhuo_shijian: '请选择进货时间',
    jinhuo_shijians: '',
    shengchan_shijian: '请选择生产日期',
    shengchan_shijians: '',
    youxiao_shijian: '请选择有效期',
    youxiao_shijians: '',
    yao_buypeo: '', //购买人
    yao_buydh: '', //购买人电话
    yao_buyadress: '', //购买人地址
    yao_gounum: '', //购买数量
    yao_xiaoshou: '', //销售人
    zd_sfzh:'',//购买人身份证号
  },
  quxiao() {
    this.setData({
      // is_shai: false,
      yao_ming: '',
      study_status: '',
      yao_pihao: '',
      yao_guige: '',
      yao_fenl: '',
      yao_scdw: '',
      zd_sfzh:'',
      yao_gong: '',
      yao_cai: '',
      yao_jinnum: '',
      yao_jintime: '',
      yao_youx: '',
      yap_piwenh: '',
      yao_buypeo: '', //购买人
      yao_buydh: '', //购买人电话
      yao_buyadress: '', //购买人地址
      yao_gounum: '', //购买数量
      isxuan: false,
      isxuans: false,
      jinhuo_shijian: '请选择进货时间',
      jinhuo_shijians: '',
      shengchan_shijian: '请选择生产日期',
      shengchan_shijians: '',
      youxiao_shijian: '请选择有效期',
      youxiao_shijians: '',
      yao_xiaoshou: '',
      satrtime: '',
      satrtimes: '开始时间',
      endtime: '',
      endtimes: '结束时间',
    })
  },
  quedi(e) {
    this.setData({
      is_shai: false
    })
    if (this.data.currtab == 0) {
      this.xiaoshou_list(1);
    }
    if (this.data.currtab == 1) {
      this.xiaoshou_list(2);
    }
  },
  // 有效期
  bindyoux(e) {
    this.setData({
      youxiao_shijian: e.detail.value,
      youxiao_shijians: e.detail.value
    })
  },
  // 生产日期
  bindshangc(e) {
    this.setData({
      shengchan_shijian: e.detail.value,
      shengchan_shijians: e.detail.value,
    })
  },
  // 进货时间
  bindjinhuo(e) {
    this.setData({
      jinhuo_shijian: e.detail.value,
      jinhuo_shijians: e.detail.value,
    })
  },
  sou_input(e) {
    let val = e.detail.value
    let modal = e.currentTarget.dataset.modal
    this.setData({
      [modal]: val
    })
  },
  xuanz(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      study_status: index
    })
    if (index == 1) {
      this.setData({
        isxuan: true,
        isxuans: false,
      })
    }
    if (index == 0) {
      this.setData({
        isxuans: true,
        isxuan: false,
      })
    }
  },
  shaix() {
    this.setData({
      is_shai: true
    })
  },
  yin_shaix() {
    this.setData({
      is_shai: false
    })
  },
  yaochn(e) {
    console.log(e)
    var index = e.detail.value;
    this.setData({
      xuany_index: index,
      yao_guo: this.data.yaolist[index]
    })
    if (index == 0) {
      this.setData({
        drugs_type: 1
      })
    }
    if (index == 1) {
      this.setData({
        drugs_type: 2
      })
    }
    if (this.data.currtab == 0) {
      this.xiaoshou_list(1);
    }
    if (this.data.currtab == 1) {
      this.xiaoshou_list(2);
    }

    // if (this.data.currtab == 0) {
    //   this.gettsyList();
    // }
    // if (this.data.currtab == 1) {
    //   this.xiaoshou_list();
    // }
  },
  tan() {
    this.setData({
      istime: !this.data.istime
    })
  },
  queren() {
    if (this.data.currtab == 0) {
      this.xiaoshou_list(1);
    }
    if (this.data.currtab == 1) {
      this.xiaoshou_list(2);
    }
    // this.gettsyList();
    this.setData({
      istime: !this.data.istime
    })
  },
  // 选择开始时间
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
      dateTime: e.detail.value,
      satrtime: e.detail.value,
      satrtimes: e.detail.value,
    })
  },
  sousuoaa(e) {
    this.setData({
      keywords: e.detail.value,
      page: 1
    })
    shuzong = []
    if (this.data.currtab == 0) {
      this.xiaoshou_list(1);
    }
    if (this.data.currtab == 1) {
      this.xiaoshou_list(2);
    }
    // if (this.data.currtab == 0) {
    //   this.gettsyList()
    // } else {
    //   this.xiaoshou_list()
    // }
  },
  qies(e) {
    shuzong = []
    shu = []
    var index = e.currentTarget.dataset.currtab
    this.setData({
      currtab: index,
      page: 1,
      yptype: '请选择药品类型',
      ypname: '',
      gg: '',
      tm: '',
      sccs: '',
      scdate: '请选择生产日期',
      yxqz: '请选择有效期截止时间',
      cpph: '',
      photo: [],
      wuphoto: '',
      date: '选择时间', //开始
      dateTime: '',
      gongwu: false,
      keywords: '',
      satrtimes: '开始时间',
      satrtime: '',
      endtimes: '结束时间',
      endtime: '',
    })
    if (this.data.currtab == 0) {
      this.xiaoshou_list(1);
    }
    if (this.data.currtab == 1) {
      this.xiaoshou_list(2);
    }
    // if (this.data.currtab == 0) {
    //   this.gettsyList();
    // }
    // if (this.data.currtab == 1) {
    //   this.xiaoshou_list();
    // }
  },
  // 查看更多
  lookmore() {
    // shuzong = this.data.tsyList
    shuzong = this.data.xiaoshoulist
    this.setData({
      page: (this.data.page) + 1
    })
    if (this.data.currtab == 0) {
      this.xiaoshou_list(1);
    }
    if (this.data.currtab == 1) {
      this.xiaoshou_list(2);
    }
    // if (this.data.currtab == 0) {
    //   this.gettsyList();
    // }
    // if (this.data.currtab == 1) {
    //   this.xiaoshou_list();
    // }
  },
  // 特殊药列表接口  台账
  gettsyList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Shop/shop_jxc_tw_alllist', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      satrtime: that.data.satrtime,
      endtime: that.data.endtime,
      drugs_type: that.data.drugs_type, //1五类药，2特效药
      keywords: that.data.keywords,
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true
            })
          }
        } else {
          if (shu.length == 0) {
            that.setData({
              tsyList: []
            })
          }
        }
        that.setData({
          tsyList: shuzong.concat(shu)
        })
      }
    })
  },
  // 选择结束时间
  endTime: function (e) {
    console.log(e)
    this.setData({
      endtime: e.detail.value,
      endtimes: e.detail.value,
    })
  },
  // 特殊药列表接口  销售
  xiaoshou_list(type) {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Shop/shop_checkout_alllist', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      satrtime: that.data.satrtime,
      endtime: that.data.endtime,
      // type: that.data.drugs_type, //1五类药，2特效药
      type: type, //1五类药，2特效药
      keywords: that.data.keywords,
      drugs_name: that.data.yao_ming,
      // drugs_class: that.data.yao_pihao,
      batch_number: that.data.yao_pihao,
      drugs_approval: that.data.yap_piwenh,
      buy_name: that.data.yao_buypeo,
      buy_mobile: that.data.yao_buydh,
      buy_address: that.data.yao_buyadress,
      buy_num: that.data.yao_gounum,
      sale_name: that.data.yao_xiaoshou,
      buy_cardnumber: that.data.zd_sfzh
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true,
            })
          }
        }
        that.setData({
          xiaoshoulist: shuzong.concat(shu)
        })
      }
    })
  },
  // 生产厂商
  yptype(e) {
    let index = e.detail.value
    this.setData({
      yptype: this.data.yptypelist[index],
    });
  },
  // 药品名称
  ypname(e) {
    this.setData({
      ypname: e.detail.value
    })
  },
  // 规格
  gg(e) {
    this.setData({
      gg: e.detail.value
    })
  },
  // 条码
  tm(e) {
    if (shuzi.test(e.detail.value) == false) {
      wx.showToast({
        title: '请输入纯数字',
        icon: 'error'
      })
      this.setData({
        tm: ''
      })
    } else {
      this.setData({
        tm: e.detail.value
      })
    }

  },
  // 生产厂商
  sccs(e) {
    this.setData({
      sccs: e.detail.value
    })
  },
  // 生产日期
  scdate: function (e) {
    this.setData({
      scdate: e.detail.value
    })
  },
  // 有效期至
  yxqz: function (e) {
    if (new Date(e.detail.value).getTime() < Date.parse(new Date())) {
      wx.showToast({
        title: '有效期不可小于当前时间',
        icon: 'none'
      })
      this.setData({
        yxqz: '请选择有效期截止时间'
      })
    } else {
      this.setData({
        yxqz: e.detail.value
      })
    }
  },
  // 产品批号
  cpph(e) {
    this.setData({
      cpph: e.detail.value
    })
  },
  // 上传
  chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.duoupimg(tempFilePaths[i]).then(res => {
                wx.showLoading({
                  title: '上传图片中...',
                });
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: wuphoto
                });
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading();
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
  // 差掉
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    var dd = wuphoto.split(",")
    console.log(dd)
    photo.splice(index, 1)
    dd.splice(index, 1)
    var rr = dd.join(",")
    this.setData({
      photo: photo,
      wuphoto: rr
    })
  },
  // 列表点击放大
  fangdas(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  // 添加点击放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  // 验证
  ti() {
    if (this.data.yptype == '请选择药品类型') {
      wx.showToast({
        title: '请选择药品类型',
        icon: 'none'
      })
    } else if (this.data.ypname == '') {
      wx.showToast({
        title: '请输入药品名称',
        icon: 'none'
      })
    } else if (this.data.gg == '') {
      wx.showToast({
        title: '请输入规格',
        icon: 'none'
      })
    } else if (this.data.tm == '') {
      wx.showToast({
        title: '请输入条码',
        icon: 'none'
      })
    } else if (this.data.sccs == '') {
      wx.showToast({
        title: '请输入生产厂商',
        icon: 'none'
      })
    } else if (this.data.scdate == '请选择生产日期') {
      wx.showToast({
        title: '请选择生产日期',
        icon: 'none'
      })
    } else if (this.data.yxqz == '请选择有效期截止时间') {
      wx.showToast({
        title: '请选择有效期截止时间',
        icon: 'none'
      })
    } else if (this.data.cpph == '') {
      wx.showToast({
        title: '请输入产品批号',
        icon: 'none'
      })
    } else if (this.data.photo.length == 0) {
      wx.showToast({
        title: '请上传留证',
        icon: 'none'
      })
    } else {
      this.que()
    }
  },
  // 添加设备
  que() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'Yiyao/yiyaoTeshuyaoAdd', {
      shopId: wx.getStorageSync('shopId'),
      name: that.data.ypname,
      catename: that.data.yptype,
      guige: that.data.gg,
      tiaoma: that.data.tm,
      changshang: that.data.sccs,
      startime: that.data.scdate,
      endtime: that.data.yxqz,
      pihao: that.data.cpph,
      liuzhao_img: that.data.wuphoto
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.gettsyList();
        setTimeout(function () {
          that.setData({
            currtab: 0,
            buttonClicked: false,
            yptype: '请选择药品类型',
            ypname: '',
            gg: '',
            tm: '',
            sccs: '',
            scdate: '请选择生产日期',
            yxqz: '请选择有效期截止时间',
            cpph: '',
            photo: [],
            wuphoto: '',
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }

    })

  },
  // 删除
  shanchu(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.dele(id)
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 删除接口
  dele(id) {
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoTeshuyaoDel', {
      id: id
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        shuzong = [];
        shu = [];
        that.gettsyList();
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      shopType: options.shopType,
      title: options.title
    })
    // this.gettsyList();
    this.xiaoshou_list(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    shuzong = []
    shu = []
    this.setData({
      yao_guo: this.data.yaolist[0],
      page: 1
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})